unit uUserDM;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uAbstractDataModule, FMTBcd, DBClient, Provider, DB, SqlExpr,uMsgControl;

type
    TUserDM = class(TAbstractDataModule)
    SqlControlUSER_ID: TIntegerField;
    SqlControlNAME: TStringField;
    SqlControlEMAIL: TStringField;
    SqlControlPHONE: TStringField;
    SqlControlLOGIN: TStringField;
    SqlControlPASSW: TStringField;
    SqlControlISADMIN: TStringField;
    cdsControlUSER_ID: TIntegerField;
    cdsControlNAME: TStringField;
    cdsControlEMAIL: TStringField;
    cdsControlPHONE: TStringField;
    cdsControlLOGIN: TStringField;
    cdsControlPASSW: TStringField;
    cdsControlISADMIN: TStringField;
    procedure cdsControlBeforePost(DataSet: TDataSet);
    procedure dspControlBeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet;
      DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
      var Applied: Boolean);
    procedure cdsControlISADMINGetText(Sender: TField; var Text: string;
      DisplayText: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  UserDM: TUserDM;

implementation
uses uUserControl;
{$R *.dfm}

procedure TUserDM.cdsControlBeforePost(DataSet: TDataSet);
begin
  inherited;
  //CheckRequiredFields(DataSet);
  if TUserControl.GetInstance.FindLoginName(
                              DataSet.FieldByName('USER_ID').AsInteger,
                              DataSet.FieldByName('LOGIN').AsString) then
    raise Exception.Create('Duplicated  login name!');
  if DataSet.State=dsInsert then
  DataSet.FieldByName('USER_ID').AsInteger:=GenerateID('GEN_USERS_ID');

end;

procedure TUserDM.cdsControlISADMINGetText(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
  inherited;
  if (Sender.Value='Y')or (Sender.Value='y') then
    Text:='YES'
  else
    Text:='NO';
end;

procedure TUserDM.dspControlBeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
  var Applied: Boolean);
  var
    msg:string;
begin
  inherited;
  if UpdateKind=ukInsert then
  begin
      msg:=('You have a new account on the Meeting Organizer!'+ #13#10+
      'Login Name: '+DeltaDS.FieldByName('LOGIN').AsString+#13#10+
      'Password: '+DeltaDS.FieldByName('PASSW').AsString);
      {msg:=(''+ #13#10+
      ''+DeltaDS.FieldByName('LOGIN').AsString+#13#10+
      ''+DeltaDS.FieldByName('PASSW').AsString); }

      TMsgControl.GetInstance.PostNotification(DeltaDS.FieldByName('EMAIL').AsString,msg)
  end;
end;

end.
